package enum_demo;

import java.util.EnumSet;
import java.util.Optional;

/**
 * Description: 枚举类示例
 *
 * @author code-worker
 * @date 2021/10/10 8:42
 */
public enum  EnumDemo {
    //枚举类对象
    ZHANG_SAN("张三", "张家的后代"),
    LI_SI("李四", "李家的后代")
    ;
    private String name;
    private String description;

    EnumDemo(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return "EnumDemo{" +
                "name='" + name + '\'' +
                ", description='" + description + '\'' +
                '}';
    }

    /**
     * task: 通过名字获取枚举对象
     * @author code-worker
     * @date 2021/10/10 8:48
     * @param name 名字
     * @return enum_demo.EnumDemo
     */
    public static EnumDemo getEnumDemoByName(String name){
        for(EnumDemo demo : EnumDemo.values()){
            if (demo.name.equals(name)) {
                return demo;
            }
        }
        return null;
    }

    public static EnumSet<EnumDemo> enumSetDemo(){
        EnumSet<EnumDemo> set = EnumSet.noneOf(EnumDemo.class);
        set.add(EnumDemo.LI_SI);
        set.add(EnumDemo.ZHANG_SAN);
        return set;
    }

    public static void main(String[] args) {
        System.out.println(EnumDemo.getEnumDemoByName("李四"));
        Optional.ofNullable(EnumDemo.getEnumDemoByName("李四"))
                .ifPresent(e -> System.out.println(e.ordinal()));
        System.out.println(EnumDemo.enumSetDemo());
    }
}
